package com.project.plugin;

import com.project.plugin.constants.DingTalkRobotConstants;
import com.project.plugin.util.NotifyUtil;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * PACKAGE      :  com.project.plugin
 * CREATE DATE  :  2018/6/9
 * AUTHOR       :  xiaoyi.xie
 * 文件描述      :  钉钉通知
 */
@Mojo(name = "dingTalk-notify", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
public class DingTalkNotifyMojo extends AbstractMojo {

    @Parameter(required = true)
    private String accessToken;
    @Parameter(required = false)
    private String atMobile;

    @Parameter(readonly = true)
    private String markdownTitle;
    @Parameter(defaultValue = "${project.groupId}", readonly = true)
    private String groupId;
    @Parameter(defaultValue = "${project.artifactId}", readonly = true)
    private String artifactId;
    @Parameter(defaultValue = "${project.version}", readonly = true)
    private String version;
    @Parameter(defaultValue = "${project.name}", readonly = true)
    private String name;
    @Parameter(defaultValue = "${project.description}", readonly = true)
    private String description;

    @Parameter(defaultValue = "${project.build.outputDirectory}/git.properties")
    private String generateGitPropertiesFilename;

    public void execute() throws MojoExecutionException, MojoFailureException {
        String[] atMobiles = new String[]{};
        if (atMobile != null) {
            atMobiles = atMobile.split(",");
        }

        if (markdownTitle == null) {
            markdownTitle = groupId.concat(":").concat(artifactId);
        }
        StringBuffer text = new StringBuffer();
        text.append("* project.groupId:").append(groupId).append(DingTalkRobotConstants.LINE_SEPARATOR);
        text.append("* project.artifactId:").append(artifactId).append(DingTalkRobotConstants.LINE_SEPARATOR);
        text.append("* project.version:").append(version).append(DingTalkRobotConstants.LINE_SEPARATOR);
        if (name != null) {
            text.append("* project.name:").append(name).append(DingTalkRobotConstants.LINE_SEPARATOR);
        }
        if (description != null) {
            text.append("* project.description:").append(description).append(DingTalkRobotConstants.LINE_SEPARATOR);
        }
        text.append(DingTalkRobotConstants.LINE_SEPARATOR);
        text.append(DingTalkRobotConstants.LINE_SEPARATOR);
        if (generateGitPropertiesFilename != null) {
            List<String> gitInfos = readTxtFileIntoStringArrList(generateGitPropertiesFilename);
            if (gitInfos != null) {
                text.append("```").append(DingTalkRobotConstants.LINE_SEPARATOR);
                for (String gitInfo : gitInfos) {
                    text.append(gitInfo).append(DingTalkRobotConstants.LINE_SEPARATOR);
                }
                text.append("```").append(DingTalkRobotConstants.LINE_SEPARATOR);
            }
        }
        NotifyUtil.sendMarkdownMessage(accessToken, markdownTitle, text.toString(), atMobiles);
    }

    private static List<String> readTxtFileIntoStringArrList(String filePath) {
        List<String> list = new ArrayList<String>();
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    list.add(lineTxt);
                }
                bufferedReader.close();
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return list;
    }

}